package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.dto.follow.FollowDto;
import com.zecan.blog.service.FollowService;
import com.zecan.blog.utils.BlogResultUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping({"/follow"})
public class FollowController {
    @Resource
    private FollowService followService;

    public FollowController() {
    }

    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')")
    @RequestMapping(
        value = {"/followUser"},
        method = {RequestMethod.POST}
    )
    public BlogResult followUser(FollowDto followDto) {
        assert followDto != null;

        this.followService.addFollow(followDto);
        return BlogResultUtils.ok("关注成功");
    }

    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')")
    @RequestMapping(
        value = {"/unFollowUser"},
        method = {RequestMethod.POST}
    )
    public BlogResult unFollowUser(FollowDto followDto) {
        assert followDto != null;

        this.followService.unFollow(followDto);
        return BlogResultUtils.ok("取消关注成功");
    }
}